package com.design.android.personnel.database;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import androidx.annotation.NonNull;
import com.design.android.personnel.bean.UserLoginLog;
import com.design.android.personnel.utils.DateUtils;

/**
 * @author admin
 */
public class UserLoginLogOperate extends BaseDatabaseOperate<UserLoginLog> {
    private static final String TABLE_NAME = "user_login_log";

    private static final String[] TABLE_FILED = new String[]{
            "id",
            "login_account",
            "login_time",
            "result",
            "result_info",
    };

    public UserLoginLogOperate(@NonNull Context context) {
        super(context);
    }

    @Override
    public void add(@NonNull UserLoginLog userLoginLog) {
        String loginTime = DateUtils.date2Str(userLoginLog.getLoginTime());

        ContentValues contentValues = new ContentValues();
        contentValues.put("login_account", userLoginLog.getLoginAccount());
        contentValues.put("login_time", loginTime);
        contentValues.put("result", userLoginLog.getResult());
        contentValues.put("result_info", userLoginLog.getResultInfo());

        if (database == null || !database.isOpen()) {
            open();
        }
        database.insert(TABLE_NAME, null, contentValues);
        database.close();
    }

    @Override
    public void delete(@NonNull Integer id) {
        if (database == null || !database.isOpen()) {
            open();
        }
        database.delete(TABLE_NAME, "id=?", new String[]{String.valueOf(id)});
        database.close();
    }

    @Override
    public void update(@NonNull UserLoginLog userLoginLog) {

    }

    @Override
    public UserLoginLog select(@NonNull Integer id) {
        if (database == null || !database.isOpen()) {
            open();
        }
        Cursor query = database.query(TABLE_NAME, TABLE_FILED,
                "id=?", new String[]{String.valueOf(id)},
                null, null, null);

        query.moveToFirst();

        UserLoginLog userLoginLog = new UserLoginLog();

        userLoginLog.setId(query.getInt(0));
        userLoginLog.setLoginAccount(query.getString(1));
        userLoginLog.setLoginTime(DateUtils.str2Date(query.getString(2)));
        userLoginLog.setResult(query.getString(3));
        userLoginLog.setResultInfo(query.getString(4));

        close();
        return userLoginLog;
    }
}
